const schema =  require('../conf/schema');
const { OK, ERR } = require('../util/util');
const ObjectID = require('mongodb').ObjectID;

const Category = schema.category;

const category = {
  init: function() {
    Category.count({}, function (err, count){
      console.log('total recored: ', count);
      if(count == 0) {
        var cs = [{
           "color": "#E040FB",
           "name": "unicorn",
           "id": "5a9ca912cacd48000184abdd"
         }, {
           "color": "#E91E63",
           "name": "Dinosaur",
           "id": "5b2cb18f31f6010001c6e671"
         }, {
           "color": "#E91E",
           "name": "Horoscope",
           "id": "5b2cae8f31f6010001c6e664"
         }, {
           "color": "#FF6D00",
           "name": "Love",
           "id": "5ac9bb523a1e9e0001e17f7f"
         }, {
           "color": "#E91E63",
           "name": "emoji (えもじ )",
           "id": "5ad05e663a1e9e0001e17f8c"
         }, {
           "color": "#2962FF",
           "name": "Shape",
           "id": "5ac9beb43a1e9e0001e17f80"
         }, {
           "color": "#4F94CD",
           "name": "Cartoon",
           "id": "5acdb88f3a1e9e0001e17f88"
         }, {
           "color": "#DD2C00",
           "name": "Magic",
           "id": "5ac9c6db3a1e9e0001e17f81"
         }, {
           "color": "#FFC125",
           "name": "Animal",
           "id": "5acdb1413a1e9e0001e17f84"
         }, {
           "color": "#FF6D00",
           "name": "Girl&Boy",
           "id": "5ac9c9e23a1e9e0001e17f82"
         }, {
           "color": "#FF8C69",
           "name": "Food",
           "id": "5acdbe803a1e9e0001e17f89"
         }, {
           "color": "#00C853",
           "name": "Life Style",
           "id": "5acafefd3a1e9e0001e17f83"
         }, {
           "color": "#039be5",
           "name": "Plant",
           "id": "5a3dc5596e1c2d0001473a65"
         }, {
           "color": "#556B2F",
           "name": "Fruit & Vegetable",
           "id": "5acdb4493a1e9e0001e17f86"
         }, {
           "color": "#EE6A50",
           "name": "Flower",
           "id": "5acdb1ce3a1e9e0001e17f85"
         }, {
           "color": "#DD2C00",
           "name": "Human",
           "id": "5a3dc5836e1c2d0001473a67"
         }];

         console.log('init category: ', cs);
         cs.forEach(function (i) {
           const c = new Category({id:i.id, name:i.name,color:i.color});
           c.save();
         });
       }
    });

  },
  findAll: function() {
    return Category.find({}, null, {sort: {id: -1}}, function (err, items) {
        if (err) {
            console.error(err);
            return ERR(500, err);
        } else {
            return items;
        }
    });
  }
};

module.exports = category;
